package cn.unknowpupil.init.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Classname: LocalTimeUtil
 * @Author: <a> w10g </a>
 * @CreateDate: Create in 2019/11/25 11:12
 * @Description:
 * @Version: 1.0
 */
public class LocalTimeUtil {
    final static Logger logger = LoggerFactory.getLogger(LocalTimeUtil.class);


    /**
     * 获取明天
     *
     * @return
     */
    public static Date nextDay() {
        // 推迟一月代码：
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.DATE, curr.get(Calendar.DATE) + 1);
        return curr.getTime();
    }

    /**
     * 获取下个月
     *
     * @return
     */
    public static Date nextMonth() {
        // 推迟一月代码：
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) + 1);
        return curr.getTime();
    }

    public static Date nextMinutes(Integer field, Integer minute) {
        // 推迟一月代码：
        Calendar curr = Calendar.getInstance();
        curr.set(field, curr.get(field) + minute);
        System.out.println(curr.getTime());
        return curr.getTime();
    }

    /**
     * 获取下个季度
     *
     * @return
     */
    public static Date nextQuarter() {
        // 推迟三月代码：
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) + 3);
        return curr.getTime();
    }

    /**
     * 更具时间推算
     *
     * @param month
     * @return
     */
    public static Date nextBanYear(int month) {
        // 推迟三月代码：
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.MONTH, curr.get(Calendar.MONTH) + month);
        return curr.getTime();
    }

    /**
     * 获取每月的天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 下一年
     *
     * @return
     */
    public static Date nextYear() {
        Calendar curr = Calendar.getInstance();
        curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 1);
        return curr.getTime();
    }

    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            logger.info(e.getMessage());
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean compare(Date time1, Date time2) {

        //Date类的一个方法，如果a早于b返回true，否则返回false
        return time1.before(time2);
		/*
		 * 如果你不喜欢用上面这个太流氓的方法，也可以根据将Date转换成毫秒
		if(a.getTime()-b.getTime()<0)
			return true;
		else
			return false;
		*/
    }

    public static String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        //sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        sdf.applyPattern("yyyy-MM-dd HH:mm");
        String out = sdf.format(new Date());
        return out;
    }


}
